package cn.xiaoxiang.user.service.impl;

import cn.xiaoxiang.basic.config.BussinessException;
import cn.xiaoxiang.basic.constants.Register;
import cn.xiaoxiang.user.domain.Logininfo;
import cn.xiaoxiang.user.domain.User;
import cn.xiaoxiang.user.dto.EmailedDto;
import cn.xiaoxiang.user.dto.UserDto;
import cn.xiaoxiang.user.mapper.LogininfoMapper;
import cn.xiaoxiang.user.mapper.UserMapper;
import cn.xiaoxiang.user.service.IUserService;
import cn.xiaoxiang.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxiang
 * @since 2022-11-24
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void registerPhone(UserDto dto) {

        //1两次密码是否一致
        if (!dto.getPassword().equals(dto.getPasswordRepeat())) {
            throw new BussinessException("两次密码不一致");
        }
        //2.电话号码是否已经注册
        User user = userMapper.finfByphone(dto.getPhone());
        if (null != user) {
            throw new BussinessException("你已经注册过了");
        }
        //3.验证码是否正确
        String o = (String) redisTemplate.opsForValue().get(String.format(Register.REGISTER_PHONE_KEY, dto.getPhone()));
        if (StringUtils.isBlank(o) || !o.equals(dto.getSmsCode())) {
            throw new BussinessException("验证码错误");
        }

        // 转换user对象
        User user1 = new User();
        user1 = dto.getUser();
        // 转换logininfo对象
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user1, logininfo);
        logininfo.setDisable(1);
        logininfo.setType(1);
        logininfoMapper.add(logininfo);
        // 添加user
        user1.setLogininfoId(logininfo.getId());
        userMapper.add(user1);
    }



    @Override
    public void registerEmail(EmailedDto emailedDto) {
        //1两次密码是否一致
        if(!emailedDto.getPassword().equals(emailedDto.getPasswordRepeat())){
            throw new BussinessException("两次密码不一致");
        }
        //2.邮箱是否已经注册
        User user= userMapper.finfByphone(emailedDto.getEmail());
        if(null!=user){
            throw new BussinessException("你已经注册过了");
        }
        //3.验证码是否正确
        String o = (String) redisTemplate.opsForValue().get(String.format(Register.REGISTER_PHONE_KEY, emailedDto.getEmail()));
        if(StringUtils.isBlank(o)||!o.equals(emailedDto.getSmsCode())){
            throw new BussinessException("验证码错误");
        }

        // 转换user对象
        User user1 = new User();
        user1=emailedDto.getUser();
        // 转换logininfo对象
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user1,logininfo);
        logininfo.setDisable(1);
        logininfo.setType(1);
        logininfoMapper.add(logininfo);
        // 添加user
        user1.setLogininfoId(logininfo.getId());
        userMapper.add(user1);
    }

}
